package com.qilin.hammer;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.CRC32;
import java.util.zip.CheckedOutputStream;

import org.apache.tools.zip.ZipEntry;
import org.apache.tools.zip.ZipOutputStream;

public class ZipUtil {
	

	private static void writeRecursive(ZipOutputStream zos, BufferedOutputStream bo,  
            File srcFile, String prefix) throws IOException, FileNotFoundException {  
        ZipEntry zipEntry;  
        String entryName = srcFile.getAbsolutePath();
        entryName = entryName.replace(prefix, "");
        if (srcFile.isDirectory()) {  
            if (!"".equals(entryName)) {  
                System.out.println("creating directory - " + srcFile.getAbsolutePath()  
                        + "  entryName=" + entryName);  
  
                zipEntry = new ZipEntry(entryName + "/");  
                zos.putNextEntry(zipEntry);  
            }
  
            File srcFiles[] = srcFile.listFiles();
            for (int i = 0; i < srcFiles.length; i++) {
                writeRecursive(zos, bo, srcFiles[i], prefix);  
            }
            srcFile.delete();
        } else {
            System.out.println("writing file- " + srcFile.getAbsolutePath() + "  entryName="  
                    + entryName);  
            BufferedInputStream bi = new BufferedInputStream(new FileInputStream(srcFile));  
  
            zipEntry = new ZipEntry(entryName);
            zos.putNextEntry(zipEntry);  
            byte[] buffer = new byte[1024];  
            int readCount = bi.read(buffer);  
  
            while (readCount != -1) {
                bo.write(buffer, 0, readCount);  
                readCount = bi.read(buffer);  
            }  
            bo.flush();
            bi.close();
            srcFile.delete();
        }  
    }  
	
	//input the uuid
	public static boolean writeToZip(String srcDir){
		String tempDirect = System.getProperty("java.io.tmpdir");
		FileOutputStream f = null;
		boolean result = true;
		try {
			f = new FileOutputStream(tempDirect+srcDir+".zip");
		} catch (FileNotFoundException e) {
			e.printStackTrace();
			result = false;
		}  
		CheckedOutputStream csum = new CheckedOutputStream(f, new CRC32());
		ZipOutputStream zos = new ZipOutputStream(csum);  
        zos.setEncoding("utf-8");
        BufferedOutputStream out = new BufferedOutputStream(zos);  
        zos.setMethod(ZipOutputStream.DEFLATED);
        File srcDirect = new File(tempDirect+srcDir+"/");
        try {
			ZipUtil.writeRecursive(zos, out, srcDirect, tempDirect);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
			result = false;
		} catch (IOException e) {
			e.printStackTrace();
			result = false;
		}
        try {
			out.close();
		} catch (IOException e) {
			e.printStackTrace();
			result = false;
		}
		return result;
	}
	
	/**
	 * @param args
	 * @throws IOException 
	 */
	public static void main(String[] args) {
		
	}

}
